package com.voice.call.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.voice.call.common.Const;
import com.voice.call.common.ConstLang;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.service.TaskIntegralService;
import com.voice.call.utils.IpUtil;
import com.voice.call.utils.JSONUtil;
import com.voice.call.version.ApiVersion;


//任务管理
@RestController
@RequestMapping("/api/{version}/task/")
public class TaskController {
	
	private final Logger log = LoggerFactory.getLogger(TaskController.class);
	
	@Resource
	private TaskIntegralService taskIntegralService;
	

	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "info", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result getTaskInfo(HttpServletRequest request,@RequestBody Map<String, Object> params) {
		//log.debug("getTaskInfo参数:{}",JSONUtil.objectToJson(params));
		
		String lang = ConstLang.getLang(params);
		if (null == params ||  params.get("uid") == null || params.get("type") == null ) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		Integer user_id = Integer.valueOf(params.get("uid").toString());
		String task_type = params.get("type").toString();
		
		if (!task_type.equals(Const.TASK_NEWS_ADVERT) && !task_type.equals(Const.TASK_VIDEO)) {
			return new Result(ResultEnum.PARAM_ERROR);
		}
		String ip = IpUtil.getIpAddr(request);
		Result rs = taskIntegralService.getTaskIntegralInfo(user_id, task_type, ip);
		rs.changeLange(lang);
		return rs;
	}
	
	
	@ResponseBody
	@ApiVersion(1)
	@RequestMapping(value = "finished", method = RequestMethod.POST, produces = Const.JSON_UTF8)
	public Result finishedTask(HttpServletRequest request,@RequestBody Map<String, Object> params) {
		//log.debug("finishedTask参数:{}",JSONUtil.objectToJson(params));
		
		String lang = ConstLang.getLang(params);
		// 判断入参
		if (null == params ||  params.get("uid") == null || params.get("id") == null || params.get("v") == null) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		
		try{
			
			String v="";
			if(params.get("v")!=null){
				v = params.get("v").toString().replaceAll("[^0-9]+", "");
				int version = Integer.valueOf(v).intValue();
				if(version < 101101){
					//log.info("版本小于1101不加积分!!!");
					return new Result(ResultEnum.SUCCESS,lang);
				}
				
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
			//log.error("版本转化为数字失败!");
			return new Result(ResultEnum.SYSTEM_ERROR,lang);
		}
		
		String ip = IpUtil.getIpAddr(request);
		Integer user_id = Integer.valueOf(params.get("uid").toString());
		Integer id = Integer.valueOf(params.get("id").toString());
		Result rs = taskIntegralService.finishedTask(user_id,id,ip);
		rs.changeLange(lang);
		return rs;
	}

}
